package com.mrogrape.notepad.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;

import com.jidesoft.swing.JideTabbedPane;
import com.mrogrape.util.StringUtils;

public class ClosableTabbedPane extends JPanel {

    /** serialVersionUID */
    private static final long serialVersionUID = 1L;


    /**
     * 构造器
     */
    public ClosableTabbedPane() {

	super();
	this.initialize();
    }

    /**
     * 添加一个子面板。
     * 
     * @param title
     * @param component
     * @return
     */
    public void addTab(ClosableTabPanel component) {

	this.tabbedPane.addTab(component.getViewableTitle(),component);
    }

    /**
     * 从容器中移除一个选项卡
     * 
     * @param component
     */
    public void removeTab(ClosableTabPanel component){

	this.tabbedPane.remove(component);
    }

    /**
     * 设置选择的面板。
     * @param comp
     */
    public void setSelectedComponent(ClosableTabPanel comp){

	int index = this.tabbedPane.indexOfComponent(comp);
	if(index > 0){

	    this.tabbedPane.setSelectedIndex(index);
	}
    }

    /**
     * 获取当前选择的面板。
     * 
     * @return
     */
    public ClosableTabPanel getSelectedComponent(){

	return  (ClosableTabPanel) this.tabbedPane.getSelectedComponent();
    }

    /**
     * 设置当前选中的选项卡
     * 
     * @param index
     */
    public void setSelectedComponentIndex(int index){

	this.tabbedPane.setSelectedIndex(index);
    }

    /**
     * 获取当前选中的选项卡面板。
     * 
     * @return
     */
    public int getSelectedComponentIndex(){

	return this.tabbedPane.getSelectedIndex();
    }

    /**
     * 获取指定index位置上的面板。
     * 
     * @return
     */
    public ClosableTabPanel getComponentAt(int index){

	return  (ClosableTabPanel) this.tabbedPane.getComponentAt(index);
    }

    /**
     * 获取全部的面板
     * 
     * @return
     */
    public List<ClosableTabPanel> getAllComponents(){

	List<ClosableTabPanel> comps = new ArrayList<ClosableTabPanel>();

	int count = this.tabbedPane.getTabCount();

	for(int i = 0; i<count;i++){

	    comps.add(this.getComponentAt(i));
	}

	return comps;
    }

    /**
     * 更新面板标题
     * 
     * @param comp
     */
    public void updateComponentTitle(ClosableTabPanel comp){

	if(StringUtils.isEmpty(comp.getViewableTitle())){

	    throw new IllegalArgumentException("Titile can not be emtpy");
	}

	int index = this.tabbedPane.indexOfComponent(comp);
	if(index < 0){
	    return;
	}
	this.tabbedPane.setTitleAt(index, comp.getViewableTitle());

    }

    /**
     * 添加一个“变更”监听器
     * @param l
     */
    public void addChangeListener(ChangeListener l){

	this.tabbedPane.addChangeListener(l);
    }

    /**
     * 移除一个“变更”监听器。
     * @param l
     */
    public void removeChangeListener(ChangeListener l){

	this.tabbedPane.removeChangeListener(l);
    }

    public JideTabbedPane getJideTabbedPane(){
	return this.tabbedPane;
    }

    private void initialize(){

	this.setLayout(new BorderLayout());
	this.tabbedPane = new JideTabbedPane(SwingConstants.TOP);
	this.tabbedPane.setShowCloseButtonOnTab(true);
	this.tabbedPane.setShowCloseButtonOnMouseOver(true);
	this.tabbedPane.setShowTabButtons(true);
	this.tabbedPane.setCloseAction(new DefualtCloseAction(this));
	//this.tabbedPane.setTabShape(JideTabbedPane.SHAPE_BOX);
	this.add(this.tabbedPane);
    }

    JideTabbedPane tabbedPane = null;

    private static class DefualtCloseAction extends AbstractAction{

	public DefualtCloseAction(ClosableTabbedPane container){

	    this.container = container;
	}

	/** serialVersionUID */
	private static final long serialVersionUID = -1L;

	@Override
	public void actionPerformed(ActionEvent evt) {

	    Object src = evt.getSource();

	    if(src instanceof ClosableTabPanel){

		ClosableTabPanel panel = (ClosableTabPanel) src;

		// 调用需要关闭窗口的onClose方法。
		boolean invokeResult = panel.onClose();

		if(invokeResult){

		    this.container.removeTab(panel);
		}
	    }
	}

	private ClosableTabbedPane container = null;

    }
}
